/**
 * 
 */
package com.demo2do.strawberry.web.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.MatrixVariable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;
import com.demo2do.strawberry.entity.startup.Startup;
import com.demo2do.strawberry.entity.startup.activity.Discussion;
import com.demo2do.strawberry.entity.startup.activity.Fans;
import com.demo2do.strawberry.entity.startup.activity.Meet;
import com.demo2do.strawberry.entity.startup.activity.Rating;
import com.demo2do.strawberry.entity.startup.category.Region;
import com.demo2do.strawberry.entity.startup.category.Tag;
import com.demo2do.strawberry.entity.user.UserRegister;
import com.demo2do.strawberry.entity.user.profile.Profile;
import com.demo2do.strawberry.service.StartupService;
import com.demo2do.strawberry.service.UserService;

/**
 * @author wilson lute
 *
 */
@Controller
@RequestMapping("/startups")
@MenuSetting("startups")
public class StartupController {
	
	@Autowired
	private StartupService startupService;
	
	@Autowired
	private UserService userService;

	/**
	 * List all the startups
	 * 
	 * @return
	 */
	@RequestMapping("")
	public ModelAndView list(Page page, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "tid", defaultValue = "0") Long tid, @RequestParam(value = "rid", defaultValue = "0") Long rid, @RequestParam(value = "fsid", defaultValue = "0") Long fsid) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-list");
		
		List<Startup> startups = startupService.list(page, name, tid, rid, fsid);
		modelAndView.addObject("startups", startups);
		modelAndView.addObject("startupCount", startups.size());
		
		List<Tag> tags = startupService.loadStartupTags();
		List<Region> regions = startupService.loadStartupRegions();
		
		modelAndView.addObject("tags", tags);
		modelAndView.addObject("regions", regions);
		modelAndView.addObject("name", name);
		modelAndView.addObject("tid", tid);
		modelAndView.addObject("rid", rid);
		modelAndView.addObject("fsid", fsid);
		
		return modelAndView;
	}
	
	/**
	 * View single startup
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}")
	public ModelAndView view(@PathVariable Long id, @Secure UserRegister userRegister, HttpServletRequest request) {

		Startup startup = startupService.load(id);

		startupService.increaseViewCount(startup);
			
		return this.viewCategory(id, "detail", userRegister, request);
	}
	
	/**
	 * 
	 * @param id
	 * @param category
	 * @return
	 */
	@RequestMapping("/{id}/{category}")
	public ModelAndView viewCategory(@PathVariable("id") Long id, @PathVariable("category") String category, @Secure UserRegister userRegister, HttpServletRequest request) {

		ModelAndView modelAndView = new ModelAndView("startup/startup-" + category);

		modelAndView.addObject("currentUri", request.getRequestURI().replaceFirst(request.getContextPath(), ""));
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);

		if(userRegister != null)
			modelAndView.addObject("isSubscribed", userService.isSubscribed(userRegister.getProfile(), startup));
		
		// get latest ratings
		List<Rating> latestRatings = startupService.getLatestRating(startup);
		modelAndView.addObject("latestRatings", latestRatings);
		
		// get latest discussions
		List<Discussion> latestDiscussions = startupService.getLatestDiscussions(startup);
		modelAndView.addObject("latestDiscussions", latestDiscussions);
		
		// get latest meets
		List<Meet> latestMeets = startupService.getLatestMeets(startup);
		modelAndView.addObject("latestMeets", latestMeets);
		
		// get latest fans
		List<Fans> latestFans = startupService.getLatestFans(startup);
		modelAndView.addObject("latestFans", latestFans);
		
		return modelAndView;
	}
	
	
	/**
	 * @param id
	 * @param page
	 * @return
	 */
	@RequestMapping("/{id}/ratings")
	public ModelAndView viewRating(@PathVariable("id") Long id, Page page, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-rating");
		
		modelAndView.addObject("currentUri", request.getRequestURI().replaceFirst(request.getContextPath(), ""));

		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		
		List<Rating> ratings = startupService.getRatings(page, startup);
		modelAndView.addObject("ratings", ratings);
		
		// get latest ratings
		List<Rating> latestRatings = startupService.getLatestRating(startup);
		modelAndView.addObject("latestRatings", latestRatings);
			
		// get latest discussions
		List<Discussion> latestDiscussions = startupService.getLatestDiscussions(startup);
		modelAndView.addObject("latestDiscussions", latestDiscussions);
				
		// get latest meets
		List<Meet> latestMeets = startupService.getLatestMeets(startup);
		modelAndView.addObject("latestMeets", latestMeets);
				
		// get latest fans
		List<Fans> latestFans = startupService.getLatestFans(startup);
		modelAndView.addObject("latestFans", latestFans);
		
		return modelAndView;
	}

	/**
	 * @param profile
	 * @param id
	 * @param rating
	 * @return
	 */
	@RequestMapping(value = "/{id}/ratings", method = RequestMethod.POST)
	public String onCreateRating(@Secure(property = "profile") Profile profile, @PathVariable("id") Long id, Rating rating) {
		
		Startup startup = startupService.load(id);
		
		startupService.createRating(rating, startup, profile);
		
		return "redirect:/startups/" + id + "/ratings";
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}/discussions")
	public ModelAndView viewDiscussion(@PathVariable("id") Long id, Page page, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-discussion");

		modelAndView.addObject("currentUri", request.getRequestURI().replaceFirst(request.getContextPath(), ""));
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		
		// load all discussions
		List<Discussion> discussions = startupService.getDiscussions(page, startup);
		modelAndView.addObject("discussions", discussions);
		
		// get latest ratings
		List<Rating> latestRatings = startupService.getLatestRating(startup);
		modelAndView.addObject("latestRatings", latestRatings);
					
		// get latest discussions
		List<Discussion> latestDiscussions = startupService.getLatestDiscussions(startup);
		modelAndView.addObject("latestDiscussions", latestDiscussions);
						
		// get latest meets
		List<Meet> latestMeets = startupService.getLatestMeets(startup);
		modelAndView.addObject("latestMeets", latestMeets);
						
		// get latest fans
		List<Fans> latestFans = startupService.getLatestFans(startup);
		modelAndView.addObject("latestFans", latestFans);
		
		return modelAndView;
	}
	
	/**
	 * @param profile
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/discussions", method = RequestMethod.POST)
	public String onCreateDiscussion(@Secure(property = "profile") Profile profile, @PathVariable("id") Long id, Discussion discussion) {
		
		Startup startup = startupService.load(id);

		startupService.createDiscussion(profile, startup, discussion);
		
		return "redirect:/startups/" + id + "/discussions";
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}/meets")
	public ModelAndView viewMeet(@PathVariable("id") Long id, Page page) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-meet");
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		
		List<Meet> meets = startupService.getMeets(page, startup);
		modelAndView.addObject("meets", meets);
		
		// get latest ratings
		List<Rating> latestRatings = startupService.getLatestRating(startup);
		modelAndView.addObject("latestRatings", latestRatings);
					
		// get latest discussions
		List<Discussion> latestDiscussions = startupService.getLatestDiscussions(startup);
		modelAndView.addObject("latestDiscussions", latestDiscussions);
						
		// get latest meets
		List<Meet> latestMeets = startupService.getLatestMeets(startup);
		modelAndView.addObject("latestMeets", latestMeets);
						
		// get latest fans
		List<Fans> latestFans = startupService.getLatestFans(startup);
		modelAndView.addObject("latestFans", latestFans);
		
		return modelAndView;
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}/create-meet")
	public ModelAndView createMeet(@PathVariable("id") Long id, @MatrixVariable(value = "tab") String tab) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-meet-create");
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		modelAndView.addObject("tab", tab);
		
		return modelAndView;
	}
	
	/**
	 * @param profile
	 * @param id
	 * @param meet
	 * @return
	 */
	@RequestMapping(value = "/{id}/meets", method = RequestMethod.POST)
	public String onCreateMeet(@Secure(property = "profile") Profile profile, @PathVariable("id") Long id, @MatrixVariable(value = "tab") String tab, Meet meet) {
		
		Startup startup = startupService.load(id);
		
		startupService.createMeet(meet, startup, profile);
		
		return "redirect:/startups/" + id + "/" + tab;
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}/fans")
	public ModelAndView viewFans(@PathVariable("id") Long id, Page page) {
		
		ModelAndView modelAndView = new ModelAndView("startup/startup-fans");
		
		Startup startup = startupService.load(id);
		modelAndView.addObject("startup", startup);
		
		// load all fans
		List<Fans> fans = startupService.getFans(page, startup);
		modelAndView.addObject("fans", fans);
		
		// get latest ratings
		List<Rating> latestRatings = startupService.getLatestRating(startup);
		modelAndView.addObject("latestRatings", latestRatings);
				
		// get latest discussions
		List<Discussion> latestDiscussions = startupService.getLatestDiscussions(startup);
		modelAndView.addObject("latestDiscussions", latestDiscussions);
						
		// get latest meets
		List<Meet> latestMeets = startupService.getLatestMeets(startup);
		modelAndView.addObject("latestMeets", latestMeets);
						
		// get latest fans
		List<Fans> latestFans = startupService.getLatestFans(startup);
		modelAndView.addObject("latestFans", latestFans);
		
		return modelAndView;
	}

	/**
	 * @param id
	 * @param profile
	 * @param printWriter
	 */
	@RequestMapping("/{id}/subscribe")
	public void subscribe(@PathVariable("id") Long id, @Secure(property = "profile") Profile profile, PrintWriter printWriter) {

		Startup startup = startupService.load(id);
		
		printWriter.write(Boolean.toString(startupService.createFan(profile, startup)));
		printWriter.flush();
		printWriter.close();
	}
	
	/**
	 * @param id
	 * @param profile
	 * @param printWriter
	 */
	@RequestMapping("/{id}/unsubscribe")
	public void unsubscribe(@PathVariable("id") Long id, @Secure(property = "profile") Profile profile, PrintWriter printWriter) {
		
		Startup startup = startupService.load(id);

		printWriter.write(Boolean.toString(startupService.removeFan(profile, startup)));
		printWriter.flush();
		printWriter.close();
	}
}
